package com.ms.qw.application.mappers.handler;

import com.ms.qw.enums.TipoCampana;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(TipoCampana.class)
public class TipoCampanaTypeHandler implements TypeHandler<TipoCampana> {

    public void setParameter(PreparedStatement preparedStatement, int i, TipoCampana item, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, item.getCodigo());
    }

    public TipoCampana getResult(ResultSet resultSet, String s) throws SQLException {
        if(StringUtils.isBlank(resultSet.getString(s))) return null;
        return TipoCampana.findByCode(resultSet.getString(s));
    }

    public TipoCampana getResult(CallableStatement callableStatement, int i) throws SQLException {
        return TipoCampana.findByCode(callableStatement.getString(i));
    }
}
